using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;

namespace ProtectThePieWP7.AdditionalClasses
{
    public class DataStore
    {
        #region Write Methods

        public static void WritePoint(XmlWriter writer, string name, Point point)
        {
            writer.WriteStartElement(name);
            writer.WriteElementString("X", point.X.ToString());
            writer.WriteElementString("Y", point.X.ToString());
            writer.WriteEndElement();
        }

        public static void WriteFloat(XmlWriter writer, string name, float number)
        {
            writer.WriteStartElement(name);
            writer.WriteValue(number);
            writer.WriteEndElement();
        }

        public static void WriteBool(XmlWriter writer, string name, bool value)
        {
            writer.WriteStartElement(name);
            writer.WriteValue(value);
            writer.WriteEndElement();
        }

        public static void WriteVector2(XmlWriter writer, string name, Vector2 vector)
        {
            writer.WriteStartElement(name);
            WriteFloat(writer, "X", vector.X);
            WriteFloat(writer, "Y", vector.Y);
            writer.WriteEndElement();
        }

        public static void WriteInt(XmlWriter writer, string name, int value)
        {
            writer.WriteStartElement(name);
            writer.WriteValue(value);
            writer.WriteEndElement();
        }

        #endregion
        #region Read Methods

        public static Vector2 ReadVector2(string name, XmlReader reader)
        {
            Vector2 result = new Vector2();

            do
            {
                // Only detect start elements.
                if (reader.IsStartElement())
                {
                    // Get element name and switch on it.
                    switch (reader.Name)
                    {
                        case "X":
                            result.X = reader.ReadElementContentAsFloat();
                            break;
                        case "Y":
                            result.Y = reader.ReadElementContentAsFloat();
                            break;
                        default:
                            reader.Read();
                            break;
                    }
                }
                else
                {
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == name) break;
                    reader.Read();
                }            
            }
            while (!reader.EOF);

            return result;
        }

        #endregion
    }
}
